package se.webbzon.oschi01.worldarea;

import java.util.ArrayList;
import java.util.Collection;

import se.webbzon.boltzmann.debug.Debug;

public class WorldLiveness {
	
	// The singleton instance of this world liveness
	private static WorldLiveness singleton;
	
	// A collection of all the registered engines
	private Collection<WorldEngine> engines;
	
	/*============================================================
	Constructors
	============================================================*/
	
	private WorldLiveness() {
		engines = new ArrayList<WorldEngine>();
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the singleton world liveness instance. **/
	public synchronized static final WorldLiveness instance() {
		if (singleton == null) {
			singleton = new WorldLiveness();
		}
		return singleton;
	}
	
	/** Returns true if the provided world engine is registered. **/
	public synchronized boolean isRegistered(WorldEngine engine) {
		return engines.contains(engine);
	}
	
	/** Returns true if the provided world engine was registered. **/
	public synchronized boolean register(WorldEngine engine) {
		int d;
		Debug.debugCollection(this, engines);
		return engines.add(engine);
	}
	
	/** Returns true if the provided world engine was deregistered. **/
	public synchronized boolean deregister(WorldEngine engine) {
		return engines.remove(engine);
	}
	
	/** Returns true if the provided world area is running by a registered
	 * world engine. **/
	public synchronized boolean isLive(WorldArea world) {
		for (WorldEngine engine : engines) {
			if (engine.getWorld() == world)
				return true;
		}
		return false;
	}

}
